require "Common/define"
require "Logic/Config"
require "Common/functions"

RedDotsWorldMapHandler = {};
local this = RedDotsWorldMapHandler;
function RedDotsWorldMapHandler.init()
    CloseEvent(Config.EventType.Fresh_WorldMap, this.update)
    ListenEvent(Config.EventType.Fresh_WorldMap, this.update)
    CloseEvent(Config.EventType.Fresh_WorldMapSearch, this.update)
    ListenEvent(Config.EventType.Fresh_WorldMapSearch, this.update)
    CloseEvent(Config.EventType.Fresh_WorldMapRevengeList, this.update)
    ListenEvent(Config.EventType.Fresh_WorldMapRevengeList, this.update) 
    CloseEvent(Config.EventType.Fresh_WorldMapReceiveRobReward, this.update)
    ListenEvent(Config.EventType.Fresh_WorldMapReceiveRobReward, this.update)
    CloseEvent(Config.EventType.refreshDefFormation, this.update);
    ListenEvent(Config.EventType.refreshDefFormation, this.update);
    return this
end

--参数1： id，nil检查所有
function RedDotsWorldMapHandler.getFlag(...)
    local msgId = ...
    local flag = Config.RedDotsFlagState.None
    if FuncOpenManager.isOpen(Config.FuncOpenType.Rob, false ) then
        if plunderHandler.IsOpen() then
            local open_chapter = dataTable.getDataTableByStringId("t_global", 330 ,"f_int_value")
            local cur_chapter = logicMgr.MapManager.GetMapcfgByID(MapManager.getMapId()).f_ChapterId or 1 
            if cur_chapter >= open_chapter   and Config.OpenPlunder == true then
                local ret1 = GetPlayerPrefs("uibigmap_click_world_map")

                local red1 = false
                local red2 = false
                local cur_times = plunderHandler.today_free_rob_times or 0
                local free_num = dataTable.getGlobalInt( 339)
                local remain_times = free_num - cur_times
                if remain_times > 0 then
                    red2 = true
                end
                local red3 = plunderHandler.GetRevengeListRedDotData()
                local red4 = plunderHandler.GetRobReceiveRewardRedDotData()
                if ret1 == nil or ret1 == "-1" then
                    red1 = true
                end
                if red1 or red2 or red3 or red4 then
                    flag = Config.RedDotsFlagState.Needful
                end
                if  logicMgr.FormationManager.checkDefFormationRedDotByType(EnumConst.FormationType.PlunderBattle) then
                    flag = Config.RedDotsFlagState.Needful;
                end 
            end
        end
    end
    return flag
end

function RedDotsWorldMapHandler.update()
    RedDotsManager.addBroadcast(Config.RedDotsType.WorldMap)
end
